package core.proto.vis.action;

import java.util.*;
import prefuse.Visualization;
import prefuse.action.GroupAction;
import prefuse.data.Edge;
import prefuse.data.Tuple;
import prefuse.data.tuple.TupleSet;
import prefuse.visual.EdgeItem;
import prefuse.visual.NodeItem;

/**
 * Only display edges whose end points are both documents 
 * in the search result. All the rest are ignored. 
 */
public class FilterSearchResultEdgeAction extends GroupAction
{
	TupleSet edges;
	static final String SOURCE = "source";
	static final String TARGET = "target";
	
    public FilterSearchResultEdgeAction(TupleSet edges) {
    	this.edges = edges;
    }

    public void run( double frac ) {
        TupleSet searchRes = m_vis.getGroup( Visualization.SEARCH_ITEMS );
        if( searchRes == null || searchRes.getTupleCount() == 0 )
            return;

        Object o = null;
        @SuppressWarnings("rawtypes")
        Iterator tuples = searchRes.tuples();
        Set<Integer> hitIds = new HashSet<Integer>();
        while( tuples.hasNext() ) {
            o = tuples.next();
            if( !(o instanceof NodeItem) ) {
                continue;
            }
            NodeItem node = (NodeItem) o;
            hitIds.add(node.getRow());
        }
        Iterator edgeItor = edges.tuples();
        while(edgeItor.hasNext()){
        	EdgeItem e = (EdgeItem) edgeItor.next();
        	if (hitIds.contains(e.getInt(SOURCE)) && hitIds.contains(e.getInt(TARGET))){
        		e.setVisible(true);
        	}
        }
    }
}
